package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.BusinessUpdateDTO;
import com.qk.entity.Business;
import com.qk.management.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @GetMapping("/pool")
    public Result BusinessPool(BusinessPoolDTO dto){
        return Result.success(businessService.businessPool(dto));
    }


    @PutMapping("/back/{id}")
    public Result back(@PathVariable Integer id){
        Business business = businessService.getById(id);
        business.setStatus(BusinessStatusConstants.RECOVERY);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }

    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        businessService.toCustomer(id);
        return Result.success();
    }

    //跟进商机
    @PutMapping
    public Result updateBusinessInfoById(@RequestBody BusinessUpdateDTO dto){
        businessService.updateBusinessInfoById(dto);
        return Result.success();
    }

    //查询回显
    @GetMapping("/{id}")
    public Result getBusinessById(@PathVariable Integer id){
        return Result.success(businessService.getBusinessById(id));
    }

    //分配商机
    @PutMapping("/assign/{businessId}/{userId}")
    public Result allocationBusiness(@PathVariable Integer businessId, @PathVariable Integer userId){
        businessService.allocationBusiness(businessId, userId);
        return Result.success();
    }

    @PostMapping
    public Result addBusiness(@RequestBody Business business){
        businessService.addBusiness(business);
        return Result.success();
    }

    @GetMapping
    public Result listBusinesses(BusinessListDTO dto){
        return Result.success(businessService.listBusinesses(dto));
    }
}
